package cc.autoapi.pucong.auto.flow.core.flow.execNode.task.system;


import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.*;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.exception.AutoDesignException;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.exec.DataBus;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.selfenum.DataTypeEnum;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.task.AbstractTask;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class SwitchTask extends AbstractTask {

    private List<NodeParamIn> paramsIns;
    private List<NodeParamOut> paramsOuts;
    private String nextNodeName;

    public SwitchTask() {
        paramsIns = new ArrayList<>();
        NodeParamIn nextCode = new NodeParamIn()
                .setParamCode("nextNodeName")
                .setDateType(DataTypeEnum.STRING)
                .setDesc("下一个执行的节点tag名称")
                .setNeedValue(true);
        paramsIns.add(nextCode);

        paramsOuts = new ArrayList<>();
    }

    @Override
    public void run() {
        ExecNode execNode = getExecNode();
        ExecParamIn taskParamIn = getParamInInfo("nextNodeName");
        Object dateValue = taskParamIn.getDateValue();
        nextNodeName = dateValue.toString();
        if (StringUtils.isBlank(nextNodeName)) {
            throw new AutoDesignException("下一个执行节点名称为空！,节点名称：" + execNode.getNodeName());
        }
    }

    @Override
    public List<TaskNode> fitterNextPublishNode(List<TaskNode> nextNodeList) {
        List<TaskNodeRelation> taskNodeRelationList = DataBus.getTaskNodeRelationList(getApiId());
        List<TaskNode> nextNodes = new ArrayList<>();
        for (TaskNode taskNode : nextNodeList) {
            TaskNodeRelation relation = taskNodeRelationList.stream().filter(taskNodeRelation -> taskNodeRelation.getRelationType() == 0 && taskNodeRelation.getNodeFromId().equals(getExecNode().getUuid()) && taskNodeRelation.getNodeToId().equals(taskNode.getUuid())).findFirst().orElse(null);
            // 不存在执行关系则跳过
            if (relation == null) {
                continue;
            }
            if (nextNodeName.equals(taskNode.getNodeTag())) {
                nextNodes.add(taskNode);
            }
        }
        // 不存则查询默认节点
        if (nextNodes.isEmpty()) {
            for (TaskNode taskNode : nextNodeList) {
                TaskNodeRelation relation = taskNodeRelationList.stream().filter(taskNodeRelation -> taskNodeRelation.getRelationType() == 0 && taskNodeRelation.getNodeFromId().equals(getExecNode().getUuid()) && taskNodeRelation.getNodeToId().equals(taskNode.getUuid())).findFirst().orElse(null);
                // 不存在执行关系则跳过
                if (relation == null) {
                    continue;
                }
                if ("default".equals(taskNode.getNodeTag())) {
                    nextNodes.add(taskNode);
                }
            }
        }
        return nextNodes;
    }

    @Override
    public String getTaskName() {
        return "选择执行节点";
    }

    @Override
    public String getTaskDesc() {
        return "根据输入参数，选择执行下一节点；输入参数编码为nextNodeName的值，为下一节点的节点tag别名，匹配则会执行该节点。";
    }

    @Override
    public String getTaskSvgIcon() {
        return "<svg t=\"1727020825165\" class=\"icon\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"46650\" width=\"32\" height=\"32\"><path d=\"M0.02816 0h1024v1024H0.02816z\" fill=\"#FFFFFF\" p-id=\"46651\"></path><path d=\"M45.937493 281.642667h761.472a47.573333 47.573333 0 0 0 45.866667-49.109334v-102.4a47.573333 47.573333 0 0 0-45.866667-49.152H45.937493A47.573333 47.573333 0 0 0 0.070827 130.133333v102.4a47.530667 47.530667 0 0 0 45.866666 49.109334z\" fill=\"#059FE8\" p-id=\"46652\"></path><path d=\"M45.937493 564.949333h494.08a142.805333 142.805333 0 0 1 31.274667-46.208l156.032-153.6H45.937493A47.573333 47.573333 0 0 0 0.070827 414.293333v101.717334a47.616 47.616 0 0 0 45.866666 48.938666z\" fill=\"#059FE8\" p-id=\"46653\"></path><path d=\"M396.273493 680.277333l32-31.402666H45.937493A47.573333 47.573333 0 0 0 0.070827 698.026667v101.717333a47.573333 47.573333 0 0 0 45.866666 49.152h325.674667a141.013333 141.013333 0 0 1 24.661333-168.618667z\" fill=\"#059FE8\" p-id=\"46654\"></path><path d=\"M558.449493 681.173333l44.501334 43.904-22.229334 21.930667a61.397333 61.397333 0 0 0-0.896 86.826667l0.896 0.896 22.229334 21.973333a63.616 63.616 0 0 0 88.064 0.938667l0.981333-0.938667 111.274667-109.781333a61.397333 61.397333 0 0 0 0.938666-86.869334l-0.938666-0.938666-22.229334-21.930667a63.573333 63.573333 0 0 0-12.8-9.173333l46.08-45.738667 44.501334 43.904a107.477333 107.477333 0 0 1 1.621333 151.978667l-1.621333 1.621333-133.504 131.584a111.232 111.232 0 0 1-154.069334 1.664l-1.706666-1.664-44.501334-43.861333a107.605333 107.605333 0 0 1-1.493333-152.106667l1.493333-1.493333z\" fill=\"#059FE8\" p-id=\"46655\"></path><path d=\"M991.64416 656.810667l-33.365333 32.938666-44.458667-43.904 22.229333-21.973333a61.44 61.44 0 0 0 0.768-86.912l-0.768-0.768-22.229333-21.930667a63.530667 63.530667 0 0 0-88.064-0.938666l-0.981333 0.938666-111.274667 109.696a61.44 61.44 0 0 0-0.938667 86.869334l0.938667 0.938666 22.229333 21.973334a64.554667 64.554667 0 0 0 12.8 9.130666l-46.08 45.738667-44.458666-43.904a107.434667 107.434667 0 0 1-1.664-151.893333l1.664-1.706667 133.504-131.584a111.232 111.232 0 0 1 154.026666-1.706667l1.749334 1.706667 44.458666 43.904a107.392 107.392 0 0 1 1.664 151.893333l-1.664 1.664z\" fill=\"#059FE8\" p-id=\"46656\"></path></svg>";
    }

    @Override
    public List<NodeParamIn> getPropertyParamsIns() {
        return paramsIns;
    }

    @Override
    public List<NodeParamOut> getPropertyParamsOuts() {
        return paramsOuts;
    }
}
